package com.personal.exception;

import com.personal.enums.CommonEnum;
import com.personal.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

//    @ExceptionHandler(Exception.class)
//    @ResponseBody
//    public Object handleException(Exception e){
//        log.error("后端执行异常，具体为：{},\n发生在：{}", e.getMessage(),e.getStackTrace());
//        return e.getMessage();
//    }

    /**
     * 请求参数反序列化失败
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result HttpMessageNotReadableException(HttpMessageNotReadableException e){
        return Result.error(CommonEnum.REQUEST_PARAM_ERROR);
    }

    /**
     * 解决单个参数时，前端不传参，报400的异常
     * 这里是解决 数值类型 传递 空字符串的情况 ""
     * @param e
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result MissingServletRequestParameterException(MissingServletRequestParameterException e){
        return Result.error(CommonEnum.REQUEST_PARAM_ERROR);
    }

    /**
     * 处理请求参数不符合要求，被validate检测到所抛出的异常
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public Result error(BindException e){
        return Result.error(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * valid 参数校验拦截返回
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<Object> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        //从异常中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getFieldErrors().get(0);
        logger.error("Code:400 参数非法:{}", objectError.getDefaultMessage());
        return Result.error(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    @ExceptionHandler(ItMallException.class)
    @ResponseBody
    public Object handleItMallException(ItMallException e){
        log.error("Code:{} - handleItMallException:{}",e.getResult().getCode(), e.getResult().getMsg());
        return e.getResult();
    }

}
